package com.iss.umservice.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.iss.umservice.bean.ResourceBean;
import com.iss.umservice.bean.RoleBean;
import com.iss.umservice.bean.RoleResourceRefBean;
import com.iss.umservice.dao.RoleDao;

/**
 * ��ɫ��ݴ����
 * 
 * @author suxaioping
 * @date 2012-8-31
 */
@Repository
public class RoleDaoImpl extends SqlMapClientDaoSupport implements RoleDao
{
    /**
     * ��ѯ�Ľ�ɫ
     * 
     * @param roleName //��ɫ���
     * @return  list
     */
    @SuppressWarnings("unchecked")
    public List<RoleBean> queryRoleInfos(String roleName, String systemId,
            int offset, int row)
    {
        //�������
        Map<String, String> paramap = new HashMap<String, String>();
        
        paramap.put("roleName", roleName);
        paramap.put("systemId", systemId);
        
        List<RoleBean> beanList = getSqlMapClientTemplate().queryForList("role.queryRoleInfos",
                paramap,
                offset,
                row);
        
        return beanList;
    }
    
    /**
     * ���ID�Ĳ�ѯ��ɫ
     * 
     * @param roleId //��ɫId
     * @return  RoleBean
     */
    public RoleBean queryRoleInfoByRoleId(String roleId)
    {
        return (RoleBean)getSqlMapClientTemplate().queryForObject("role.queryRoleInfoByRoleId",
                roleId);
    }
    
    /**
     * ��ݽ�ɫID�Ĳ�ѯ��Դ
     * 
     * @param roleId //��ɫId
     * @return  ResourceBean
     */
    @SuppressWarnings("unchecked")
    public List<ResourceBean> queryResourceInfoByRoleId(String roleId)
    {
        List<ResourceBean> beanList = getSqlMapClientTemplate().queryForList("role.queryResourceByRoleId",
                roleId);
        
        return beanList;
    }
    
    /**
     * ���ӽ�ɫ��Ϣ
     * 
     * @param roleBean //��ɫ��Ϣ
     * @return  
     */
    public void addRoleInfo(RoleBean roleBean)
    {
        //���ӽ�ɫ��Ϣ
        getSqlMapClientTemplate().insert("role.insertRoleInfo", roleBean);
        
        //���ӽ�ɫ����Դ�Ĺ�ϵ
        if (null != roleBean.getResourceId()
                && !"".equals(roleBean.getResourceId()))
        {
            
            String[] resourceIds = roleBean.getResourceId().split(",");
            
            for (int i = 0; i < resourceIds.length; i++)
            {
                RoleResourceRefBean bean = new RoleResourceRefBean();
                
                bean.setResourceId(resourceIds[i]);
                bean.setRoleId(roleBean.getRoleId());
                
                //���ӽ�ɫ����Դ��Ӧ��ϵ
                getSqlMapClientTemplate().insert("role.insertRole_Resource_Ref",
                        bean);
            }
        }
        
    }
    
    /**
     * ɾ���ɫ��Ϣ
     * 
     * @param roleId //��ɫID
     * @return  
     */
    public void delRoleInfo(String roleId)
    {
        
        //ɾ���ɫ����Դ��Ӧ��ϵ
        getSqlMapClientTemplate().delete("role.delRole_Resource_Ref", roleId);
        
        //ɾ���ɫ���λ�Ĺ�ϵ
        getSqlMapClientTemplate().delete("role.delRole_Station_Ref", roleId);
        
        //ɾ���ɫ��Ϣ
        getSqlMapClientTemplate().delete("role.delRoleInfo", roleId);
        
    }
    
    /**
     * �޸Ľ�ɫ��Ϣ
     * 
     * @param roleBean //��ɫ��Ϣ
     * @return  
     */
    public void updateRoleInfo(RoleBean roleBean)
    {
        //�޸Ľ�ɫ��Ϣ
        getSqlMapClientTemplate().update("role.updateRoleInfo", roleBean);
        
        //ɾ���ɫ����Դ��Ӧ��ϵ
        getSqlMapClientTemplate().delete("role.delRole_Resource_Ref",
                roleBean.getRoleId());
        
        //������ɫ����Դ��Ӧ��ϵ
        if (null != roleBean.getResourceId()
                && !"".equals(roleBean.getResourceId()))
        {
            
            String[] resourceIds = roleBean.getResourceId().split(",");
            
            for (int i = 0; i < resourceIds.length; i++)
            {
                RoleResourceRefBean bean = new RoleResourceRefBean();
                
                bean.setResourceId(resourceIds[i]);
                bean.setRoleId(roleBean.getRoleId());
                
                //���ӽ�ɫ����Դ��Ӧ��ϵ
                getSqlMapClientTemplate().insert("role.insertRole_Resource_Ref",
                        bean);
            }
        }
        
    }
    
    /**
     * �������ͳ�ƽ�ɫ
     * 
     * @param roleName ��ɫ���
     * @return int
     */
    public int countRoleInfo(String roleName, String systemId)
    {
        //�������
        Map<String, String> paramap = new HashMap<String, String>();
        
        paramap.put("roleName", roleName);
        paramap.put("systemId", systemId);
        
        int count = ((Integer)getSqlMapClientTemplate().queryForObject("role.countRoleInfo",
                paramap)).intValue();
        
        return count;
    }

    public String checkRoleName(String roleName)
    {
        // TODO Auto-generated method stub
        return (String)getSqlMapClientTemplate().queryForObject("role.checkRoleName",roleName);
    }

    @Override
    public void updateRoleName(RoleBean roleBean)
    {
        // TODO Auto-generated method stub
        getSqlMapClientTemplate().update("role.updateRoleName",roleBean);
    }
    
}
